# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
from http import HTTPStatus
import random
from typing import List, Optional

from knowledge_qa_llm.utils import  logger

import dashscope


class DashscopeBot:
    """
     阿里的灵积模型平台:https://help.aliyun.com/zh/dashscope
    """
    def __init__(self, api_type: str = None, access_token: str = None,**kwargs):
        self.api_type = api_type
        dashscope.api_key = access_token

    def __call__(self, prompt: str, history: Optional[List] = None, **kwargs):
        response=dashscope.Generation.call(
            model=self.api_type,
            prompt=prompt,
            seed=random.randint(0, 10000),
            result_format='text'
        )
        if response.status_code==HTTPStatus.OK:
            result=response.get('output',{}).get('text','无获取json的text结果')
            logger.info(f'调用阿里model:{self.api_type},结果:{result}')
        else:
            logger.error('调用阿里接口异常:'+response.message)
            result=None
        return result
